#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <poll.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <errno.h>
#include <ctype.h>
#define OPEN_MAX 1024
#define BUF_MAX 80

void sys_err(const char* str)
{
    perror(str);
    exit(1);
}

int main(int argc,char* argv[])
{
    int sockfd,new_sockfd,count,n,j,ret;
    struct sockaddr_in server_addr,client_addr;
    struct pollfd client[OPEN_MAX];
    char buf[BUF_MAX];
    socklen_t addr_len;
    //bzero(&server_addr,sizeof(server_addr));
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
        sys_err("socket()");
    int opt = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    ret = bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
    if(ret < 0)
        sys_err("bind()");
    ret = listen(sockfd,10);
    if(ret < 0)
        sys_err("bind()");
    client[0].fd = sockfd;
    client[0].events = POLLIN;
    int i = 0,maxi = 0;
    for(i=1;i<OPEN_MAX;i++)
    {
        client[i].fd = -1;
    }
    while(1)
    {
        count = poll(client,maxi+1,-1);
        if(client[0].revents & POLLIN)
        {
            addr_len = sizeof(client_addr);
            new_sockfd = accept(sockfd,(struct sockaddr*)&client_addr,&addr_len);
            if(new_sockfd < 0)
                sys_err("accept()");
            for(i=1;i<OPEN_MAX;i++)
            {
                if(client[i].fd < 0)
                {
                    client[i].fd = new_sockfd;
                    break;
                }
            }
            if(i == OPEN_MAX)
            {
                sys_err("too many");
            }

            client[i].events = POLLIN;
            if(maxi < i)
                maxi = i;
            if(--count <= 0)
            {
                continue;
            }
        }
        for(i=1;i<=maxi;++i)
        {
            if(client[i].fd < 0)
                continue;
            if(client[i].revents & POLLIN)
            {
                if((n=read(client[i].fd,buf,sizeof(buf))) < 0)
                {
                    if(errno == ECONNRESET)
                    {
                        close(client[i].fd);
                        client[i].fd = -1;
                    }
                    else
                        sys_err("read()");
                }
                else if(n == 0)
                {
                    close(client[i].fd);
                    client[i].fd = -1;
                }
                else
                {
                    for(j=0;j<n;j++)
                    {
                        buf[j] = toupper(buf[j]);
                    }
                    ret = write(client[i].fd,buf,n);
                    if(ret < 0)
                        sys_err("write()");
                    ret = write(STDOUT_FILENO,buf,n);
                    if(ret < 0)
                        sys_err("write()");
                }
                if(--count <= 0)
                    break;
            }
        }
    }
    return 0;
}